home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
pcr
/
pcr4_4.lha
/
DIST
/
threads
/
ThreadsSharedMemBSDUtils.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-11-28
|
4KB
|
165 lines
/* begincopyright
Copyright (c) 1988 Xerox Corporation. All rights reserved.
Use and copying of this software and preparation of derivative works based
upon this software are permitted. Any distribution of this software or
derivative works must comply with all applicable United States export
control laws. This software is made available AS IS, and Xerox Corporation
makes no warranty about the software, its performance or its conformity to
any specification. Any person obtaining a copy of this software is requested
to send their name and post office or electronic mail address to:
PCR Coordinator
Xerox PARC
3333 Coyote Hill Rd.
Palo Alto, CA 94304
endcopyright */
/*
* ThreadsSharedMemBSDUtils.c
*
* Demers, November 28, 1990 11:09:56 am PST
*
*/
#include "xr/BasicTypes.h"
#include "xr/ThreadsSharedMemBSDUtils.h"
#include "xr/Errno.h"
#include <string.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>
extern void bcopy(/* char *from, char *to, int nbytes */);
static char
XR_sharedMemBSDSwapDir[FILE_NAME_BUF_SIZE] = DEFAULT_SWAP_DIR;
static int
XR_sharedMemBSDSwapDirIndex = DI_NONE;
int
XR_SharedMemBSDSetSwapDir(dir, index)
char *dir;
int index;
{
if( dir != NIL ) {
if( strlen(dir) >= (sizeof XR_sharedMemBSDSwapDir) )
return (-ENAMETOOLONG);
(void)strcpy(XR_sharedMemBSDSwapDir, dir);
}
XR_sharedMemBSDSwapDirIndex = index;
return 0;
}
int
XR_SharedMemBSDGetSwapDir(dirBuf, dirBufLen, indexBuf)
char *dirBuf;
int dirBufLen;
int *indexBuf;
{
if( dirBuf != NIL ) {
if( strlen(XR_sharedMemBSDSwapDir) >= dirBufLen )
return (-ENAMETOOLONG);
strcpy(dirBuf, XR_sharedMemBSDSwapDir);
}
if( indexBuf != NIL ) {
*indexBuf = XR_sharedMemBSDSwapDirIndex;
}
return 0;
}
static char *
XR_SharedMemBSDAppendNum(buf, num)
char *buf;
unsigned num;
{
char b[12];
char *pb = b + (sizeof b);
*--pb = 0;
for(;;) {
*(--pb) = ('0' + (num % 10));
if( (num /= 10) == 0 ) break;
}
for(;;) {
if( (*buf = *(pb++)) == 0 ) return buf;
buf++;
}
}
int /* -errno */
XR_SharedMemBSDMkFileName(buf, bufLen, shortName, fileIndex)
char *buf;
int bufLen;
char *shortName;
int fileIndex;
{
int dirNameLen, shortNameLen;
char *p = buf;
/* not done with sprintf because can be called with very */
/* limited stack space on panic ... */
dirNameLen = strlen(XR_sharedMemBSDSwapDir);
shortNameLen = ( (shortName != NIL) ? strlen(shortName) : 0 );
if( (dirNameLen + shortNameLen + FILE_NAME_SLOP) > bufLen )
return (-ENAMETOOLONG);
(void)strcpy(p, XR_sharedMemBSDSwapDir); p += dirNameLen;
if( XR_sharedMemBSDSwapDirIndex == DI_NONE ) return 0;
*p++ = '/';
p = XR_SharedMemBSDAppendNum(p, XR_sharedMemBSDSwapDirIndex);
if( shortNameLen == 0 ) return 0;
*p++ = '/';
(void)strcpy(p, shortName); p += shortNameLen;
if( fileIndex != FI_NONE ) p = XR_SharedMemBSDAppendNum(p, fileIndex);
return 0;
}
int
XR_SharedMemBSDGetBackingFile(shortName, index, setLen, fdp)
char *shortName;
int index;
int setLen; /* < 0 ==> don't change */
int *fdp;
{
int fd, ans;
bool stickyFailed = FALSE;
struct stat statBuf;
char fnBuf[FILE_NAME_BUF_SIZE];
ans = XR_SharedMemBSDMkFileName(fnBuf, (sizeof fnBuf), shortName, index);
if( ans < 0 ) return (ans);
fd = open(fnBuf, O_RDWR, 0666);
if( fd < 0 ) {
fd = open(fnBuf, O_RDWR|O_CREAT, 0666);
if( fd < 0 ) return (-EIO);
}
ans = fstat(fd, &statBuf);
if( ans < 0 ) return (-EIO);
if( (statBuf.st_mode & S_ISVTX) == 0 ) {
if( geteuid() == 0 ) {
ans = fchmod( fd, (statBuf.st_mode | S_ISVTX) );
stickyFailed = (ans != 0);
} else {
stickyFailed = TRUE;
}
}
if( (setLen >= 0) && (statBuf.st_size != setLen) ) {
ans = ftruncate(fd, setLen);
if( ans < 0 ) { (void)close(fd); return (-EIO); }
}
*fdp = fd;
return (stickyFailed ? 1 : 0);
}